/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package battleship;

import java.util.Collection;
import java.util.HashSet;

/**
 *
 * @author Tomek
 */
public class Ship {
    ShipType type;
    Collection<Coords> shipPoints = new HashSet<Coords>();

	Ship(Integer size) {
		type = ShipType.getShip(size);
	}

	Ship(int actualShipSize, Collection<Field> set) {
		type = ShipType.getShip(actualShipSize);
		for(Field f:set){
			shipPoints.add(new Coords(f.col, f.row, this));
		}
	}
    public boolean isSunk(){
        for(Coords c: shipPoints){
            if(!c.isHit())return false;
        }
        return true;
    }

	void hit(Coords c) throws ShipSunkException {
		if (shipPoints.contains(c)) {
			for (Coords d : shipPoints) {
				if (c.equals(d)) {
					d.hit();
				}
			}
			if (isSunk())
				throw new ShipSunkException();
		} else
			return;
	}

	@Override
	public String toString() {
		return type.toString();
	}
}
